package com.tencent.lucky.external_plugin

import android.app.Activity
import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Rect
import android.util.Log
import android.view.Surface
import androidx.annotation.NonNull
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.embedding.engine.plugins.activity.ActivityAware
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result
import io.flutter.plugin.common.PluginRegistry.Registrar
import io.flutter.view.TextureRegistry


/** ExternalPlugin */
class ExternalPlugin: FlutterPlugin, MethodCallHandler, ActivityAware {



  companion object {
    @JvmStatic
    fun registerWith(registrar: Registrar) {
      val channel = MethodChannel(registrar.messenger(), "external_plugin")
      channel.setMethodCallHandler(ExternalPlugin())
    }

    @JvmField
    var ExternalPluginLogName : String = "ExternalPlugin"

  }

  private lateinit var channel : MethodChannel
  private lateinit var textureRegistry: TextureRegistry
  private lateinit var surfaceEntry: TextureRegistry.SurfaceTextureEntry
  private lateinit var externalGLThread: ExternalGLThread
  private lateinit var context: Context
  private lateinit var activity: Activity


  override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
    channel = MethodChannel(flutterPluginBinding.flutterEngine.dartExecutor, "external_plugin")
    channel.setMethodCallHandler(this)
    textureRegistry = flutterPluginBinding.textureRegistry
    context = flutterPluginBinding.applicationContext

  }


  override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) {
    val arguments = call.arguments<Map<String,Int>>()
    when (call.method) {
      "create" -> {
        val width = arguments["width"] as Int
        val height = arguments["height"] as Int

        /// 获取图片地址
        val url = arguments["url"]

        //图片下载过程/读取Native本地icon
        val bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.hotel) as Bitmap

        surfaceEntry = textureRegistry.createSurfaceTexture()
        val textureId = surfaceEntry.id()
       /// 创建Surface实例加载surfaceTexture

        val surface = Surface(surfaceEntry.surfaceTexture().apply {
          setDefaultBufferSize(width!!, height!!)
        })
        /// 画布绘制bitmap 纹理映射
        val srcRect = Rect(0,0,bitmap.width,bitmap.height)
        val desRect = Rect(0,0,width,height)
        val canvas = surface.lockCanvas(desRect)

        canvas.drawBitmap(bitmap, srcRect, desRect, null)

        bitmap.recycle()
        surface.unlockCanvasAndPost(canvas)
        result.success(textureId)
      }
      "dispose" -> {
        externalGLThread.dispose()
        surfaceEntry.release()
      }
      else -> result.notImplemented()
    }
  }

  override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
    channel.setMethodCallHandler(null)
  }

  override fun onDetachedFromActivity() {
    TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
  }

  override fun onReattachedToActivityForConfigChanges(binding: ActivityPluginBinding) {
    TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
  }

  override fun onDetachedFromActivityForConfigChanges() {
    TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
  }

  override fun onAttachedToActivity(binding: ActivityPluginBinding) {
    activity = binding.activity
    Log.i(ExternalPluginLogName, activity.toString())
  }
}
